package com.gxuwz.com_property.repair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.com_property.common.dto.ResponseDto;
import com.gxuwz.com_property.repair.dto.RepairDto;
import com.gxuwz.com_property.repair.pojo.Repair;
import com.gxuwz.com_property.repair.service.RepairService;
import com.gxuwz.com_property.user.pojo.User;
import com.gxuwz.com_property.utils.RamdomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
public class RepairController {

    @Autowired
    private RepairService repairService;
    /**
     * 随机数
     * */
    @Autowired
    private RamdomUtils ramdomUtils;
    /**
     * 添加报修
     * */
    @PostMapping("/addRepairInfo")
    public ResponseDto addRepairInfo(@RequestBody Repair repair) {
        long repair_id = ramdomUtils.generateId();
        repair.setId(repair_id);

        if (repair.getSubmitTime()!=null){
            // 原始日期时间值
            String originalDateTime = repair.getSubmitTime();
            // 将原始日期时间转换为 LocalDateTime 对象，并调整时区（这里假设您想要转换为系统默认时区）
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.parse(originalDateTime), ZoneId.systemDefault());
            // 将 LocalDateTime 对象转换为符合 MySQL 的日期时间格式
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String mysqlDateTime = dtf.format(localDateTime);
            repair.setSubmitTime(mysqlDateTime);
        }

        Boolean aBoolean = repairService.addRepairInfo(repair);

        if (aBoolean){
            return ResponseDto.success("提交报修单成功");
        }else {
            return ResponseDto.error("提交报修单失败");
        }

    }
    /**
     * 修改报修====== 处理报告
     * */
    @PostMapping("/updateRepairInfo")
    public ResponseDto updateRepairInfo(@RequestBody Repair repair) {
        System.out.println("updateRepairInfo+Repair:"+repair);


        if (repair.getSubmitTime()!=null){
            // 原始日期时间值
            String originalDateTime = repair.getSubmitTime();
            // 将原始日期时间转换为 LocalDateTime 对象，并调整时区（这里假设您想要转换为系统默认时区）
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.parse(originalDateTime), ZoneId.systemDefault());
            // 将 LocalDateTime 对象转换为符合 MySQL 的日期时间格式
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String mysqlDateTime = dtf.format(localDateTime);
            repair.setSubmitTime(mysqlDateTime);
        }

        Boolean aBoolean = repairService.updateRepairInfo(repair);

        if (aBoolean){
            return ResponseDto.success("提交报修单成功");
        }else {
            return ResponseDto.error("提交报修单失败");
        }
    }
    /**
     * 删除报修报告
     * */

    public ResponseDto delectRepairById(int id) {

        Boolean aBoolean = repairService.delectRepairById(id);

        if (aBoolean){
            return ResponseDto.success("删除报修单成功");
        }else {
            return ResponseDto.error("删除报修单失败");
        }
    }
    /**
     * 查询报修报告
     * */
    public ResponseDto selectRepairRepair(Repair repair) {
        Repair repair1 = repairService.selectRepairRepair(repair);
        if (repair1!=null){
            return ResponseDto.success("查询报修单成功",repair1);
        }else {
            return ResponseDto.error("查询报修单失败",repair1);
        }
    }

    /**
     * 查询维修报告图表
     * */
    @PostMapping("/getRepairMangene")
    public ResponseDto RepairChartMangene(){
        List<Map<String, Object>> maps = repairService.RepairChartMangene();
        if (!maps.isEmpty()){
            return ResponseDto.success("查询维修报告图表成功",maps);
        }else {
            return ResponseDto.error("查询维修报告图表失败");
        }
    }

    /**
     * 分页查询报告
     * */
    @PostMapping("/selectRepairsByPage")
    public ResponseDto selectRepairByPage(@RequestBody RepairDto repairDto) {
        System.out.println("selectRepairByPage:repairDto"+repairDto);
        Integer pageNo = repairDto.getPageNo();
        Integer pageSize = repairDto.getPageSize();
        String handleState = null;  //请求状态
        String flag = null;  //响应状态
        IPage<Repair> repairIPage = null;

        if (repairDto.getHandleState()!=null){
            handleState = repairDto.getHandleState();

            System.out.println("handleState:"+handleState);

            if (handleState.equals("all") || handleState.equals("")){
                repairDto.setHandleState("");
                flag = "查询所有报修单成功";
            }else if (handleState.equals("untreated")){
                repairDto.setHandleState("3");
                flag = "查询未处理的报修单成功";
            }else if (handleState.equals("processing")){
                repairDto.setHandleState("2");
                flag = "查询处理中的报修单成功";
            }else if (handleState.equals("processed")){
                repairDto.setHandleState("1");
                flag = "查询处理完成的报修单成功";
            }

        }

        /**
         * 执行业务逻辑
         * */
        repairIPage = repairService.selectRepairByPage(new Page<Repair>(pageNo, pageSize), repairDto);

        if (repairIPage!=null){
            return ResponseDto.success(flag,repairIPage);
        } else {
            return ResponseDto.error("查询报修单失败",repairIPage);
        }
    }
}
